package com.tanhua.server.controller;

import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Comments;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description: 查询评论列表
 * @author: Administrator
 * @date: Created in 2021/2/22 9:59
 * @version: 0.0.1
 * @modified By:2
 */
@RequestMapping("comments")
@RestController
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    @GetMapping
    public ResponseEntity<PageResult>queryCommentsList(@RequestParam("movementId")String publishId,
                                                       @RequestParam(value ="page",defaultValue = "1")Integer page,
                                                       @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult=  this.commentsService.queryCommentsList(publishId,page,pageSize);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 保存评论
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping
    public ResponseEntity<Void>saveComments(@RequestBody Map<String,String>param){
        try {
            String publishId = param.get("movementId");
            String content = param.get("comment");
            Boolean bool=this.commentsService.saveComments(publishId,content);
            if(bool){
                //发送评论消息
                this.quanziMQService.commentPublishMsg(publishId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 评论点赞
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long>likeComment(@PathVariable("id")String publishId){
        try {
            Long count = this.movementsService.likeComment(publishId);
            if(null!=count){
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 取消评论点赞
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long>disLikeComment(@PathVariable("id")String publishId){
        try {
            Long count = this.movementsService.disLikeComment(publishId);
            if(null!=count){
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
